package com.demo.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.text.View;

import com.demo.data.Data;
import com.demo.data.DatabseHandler;

public class LoginGui extends JFrame implements ActionListener
{
	private JTextField taUserName;
	private JTextField taPassword;
	private JButton btnLogin;
	
	public LoginGui() 
	{
		JPanel jpCenter = new JPanel();
		getContentPane().add(jpCenter, BorderLayout.NORTH);
		GridBagLayout gbl_panelCenter = new GridBagLayout();
		gbl_panelCenter.columnWidths = new int[] { 0, 0};
		gbl_panelCenter.rowHeights = new int[] { 0, 0, 0 };
		jpCenter.setLayout(gbl_panelCenter);
		
		JLabel label = new JLabel("Username");
		GridBagConstraints gbc_lblUser = new GridBagConstraints();
		gbc_lblUser.anchor = GridBagConstraints.WEST;
		gbc_lblUser.insets = new Insets(5, 0, 0, 5);
		gbc_lblUser.gridx = 0;
		gbc_lblUser.gridy = 0;
		jpCenter.add(label, gbc_lblUser);

		
		taUserName = new JTextField();
		GridBagConstraints gbc_tfUser = new GridBagConstraints();
		gbc_tfUser.insets = new Insets(5, 0, 0, 5);
		gbc_tfUser.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfUser.gridx = 1;
		gbc_tfUser.gridy = 0;
		jpCenter.add(taUserName, gbc_tfUser);
		taUserName.setColumns(10);
		
		label = new JLabel("Password");
		GridBagConstraints gbc_lblTmp = new GridBagConstraints();
		gbc_lblTmp.insets = new Insets(5, 0, 0, 5);
		gbc_lblTmp.gridx = 0;
		gbc_lblTmp.gridy = 1;
		jpCenter.add(label, gbc_lblTmp);
		
		taPassword = new JTextField();
		GridBagConstraints gbc_tfPassword = new GridBagConstraints();
		gbc_tfPassword.insets = new Insets(5, 0, 0, 5);
		gbc_tfPassword.fill = GridBagConstraints.NONE;
		gbc_tfPassword.gridx = 1;
		gbc_tfPassword.gridy = 1;
		jpCenter.add(taPassword, gbc_tfPassword);
		taPassword.setColumns(10);
		
		btnLogin = new JButton("Login");
		btnLogin.addActionListener(this);
		GridBagConstraints gbc_btnLogin = new GridBagConstraints();
		gbc_btnLogin.insets = new Insets(5, 0, 0, 5);
		gbc_btnLogin.gridx = 1;
		gbc_btnLogin.gridy = 2;
		gbc_btnLogin.fill = GridBagConstraints.HORIZONTAL;
		jpCenter.add(btnLogin, gbc_btnLogin);
		
		getContentPane().add(jpCenter);
		setSize(250, 150);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int desktopWidth = (int) screenSize.getWidth();
		int desktopHeight = (int) screenSize.getHeight();
		setLocation((desktopWidth - 250)/2, (desktopHeight - 150)/2);
		setTitle("Login");
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}	
	
	@Override
	public void actionPerformed(ActionEvent e)
	{
		requsetLogin();
	}
	public void requsetLogin() 
	{
		try
		{
			String userName = taUserName.getText();
			String password = taPassword.getText();
			int requestCode = Data.REQUEST_LOGIN;
			DataOutputStream dos = new DataOutputStream(Client.clientSocket.getOutputStream());
			dos.writeInt(requestCode);
			dos.writeUTF(userName + "/" + password);
			dos.flush();
			dos.close();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
}
